<template>
  <div class="login">
    <div class="content">
      <div class="logo">
        <img src="@/assets/images/login-logo.png" alt="" />
      </div>
      <van-form @submit="onSubmit" class="form">
        <van-cell-group class="item">
          <van-field
            v-model="user.mobile"
            :rules="rules.mobile"
            placeholder="请输入手机号码"
          >
            <template #left-icon>
              <i class="iconfont icon-shouji"></i>
            </template>
          </van-field>
        </van-cell-group>
        <van-cell-group class="item">
          <van-field
            v-model="user.code"
            :rules="rules.code"
            placeholder="验证码"
          >
            <template #button>
              <van-button type="info" size="normal" native-type="button"
                >获取验证码</van-button
              >
            </template>
            <template #left-icon>
              <i class="iconfont icon-yanzhengma"></i>
            </template>
          </van-field>
        </van-cell-group>
        <van-button
          round
          type="info"
          size="large"
          class="formbtn"
          :loading="isLoading"
          >进入胖虎头条</van-button
        >
      </van-form>
    </div>
  </div>
</template>

<script>
import { userLogin } from '@/api/login.js'
import { saveLocal } from '@/utils/local.js'
export default {
  data () {
    return {
      isLoading: false,
      user: {
        mobile: '',
        code: ''
      },
      rules: {
        mobile: [
          { required: true, trigger: 'onBlur' },
          {
            pattern: /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/,
            message: '请输入正确的手机号码',
            trigger: 'onBlur'
          }
        ],
        code: [
          { required: true, trigger: 'onBlur' },
          { pattern: /^\d{6}$/, message: '验证码为6位数字', trigger: 'onBlur' }
        ]
      }
    }
  },
  methods: {
    // 表单提交按钮
    async onSubmit () {
      if (this.isLoading) {
        return
      }
      try {
        this.isLoading = true
        const res = await userLogin(this.user)
        this.$store.commit('setUserInfo', res.data.data)
        saveLocal('userInfo', JSON.stringify(res.data.data))
        if (this.$route.path === '/login') {
          this.$router.push('/home')
        } else {
          this.$router.back()
        }
      } catch {
        this.$toast.fail('登录失败,您输入的手机号或验证码有误')
      }
      this.isLoading = false
    }
  }
}
</script>

<style lang="less" scoped>
.login {
  background: url(../../assets/images/login-bg.png) no-repeat;
  background-size: contain;
  padding: 10px;
  .content {
    position: relative;
    width: 300px;
    height: 382px;
    background: #ffffff;
    border-radius: 10px;
    box-shadow: 0px 5px 20px 0px rgba(0, 0, 0, 0.1);
    margin: 139px auto 0;
    .logo {
      width: 100%;
      height: 55px;
      text-align: center;
      padding-top: 23px;
      img {
        width: 55px;
        height: 55px;
      }
    }
    .form {
      position: relative;
      width: 260px;
      height: 240px;
      padding: 60px 20px 0;
      .item {
        .van-cell {
          padding: 0;
        }

        margin-bottom: 17px;
        /deep/ input {
          text-indent: 1em;
        }
        .iconfont {
          color: #a3d0fc;
          font-size: 16px;
        }
        /deep/ .van-button {
          width: 97px;
          height: 35px;
          font-size: 12px;
          border-radius: 4px;
        }
      }
      [class*='van-hairline']::after {
        border: none;
        border-bottom: 1px solid #3296fa;
      }
      .formbtn {
        position: absolute;
        right: 0;
        left: 0;
        bottom: -25px;
        margin: auto;
        width: 260px;
        font-size: 18px;
      }
    }
  }
}
</style>
